package main

import "sync"
import "fmt"

func main() {
	//muLock()
	//syncCh()
	//asyncCh()
	//multiGorouting()
	waitGroup()
}

func muLock() {
	var mu sync.Mutex
	mu.Lock()
	go func() {
		fmt.Println("hello world!")
		mu.Unlock()
	}()
	mu.Lock()
}

func syncCh() {
	//无缓冲通道，接收早于发送
	done := make(chan int)
	go func() {
		fmt.Println("hello world")
		<-done
	}()
	//写入后阻塞等待读
	done<-1
}

func asyncCh() {
	//有缓冲通道，同步事件不受通道是否有缓存影响
	done := make(chan int, 2)
	go func() {
		fmt.Println("hello world")
		done<-1
	}()
	<-done
}

func multiGorouting() {
	done := make(chan int, 10)
	//开启N个打印线程
	for i := 0; i < cap(done); i++ {
		go func() {
			fmt.Println("hh")
			done<-1
		}()
	}

	//等待N个线程完成
	for i := 0; i < cap(done); i++ {
		<-done
	}

}


func waitGroup() {
	var wg sync.WaitGroup
	//开N个打印线程
	for i := 0; i<10; i++ {
		wg.Add(1)
		go func() {
			fmt.Println("llh")
			wg.Done()
		}()
	}
	//等待N个线程完成
	wg.Wait()
}